package wstrader.task.trade.futures;

import java.sql.ResultSet;
import java.util.Vector;

import pc.sql.value.*;

import pc.system.*;
import pc.util.*;
import wstrader.task.trade.TradeTask;

public class Transactions extends TradeTask
{
  public Transactions(Tasks owner)
  {
    super(owner);
  }
  
  public long doTask()
  {
    String table_name = "futures_contract";
    int task_id     = this.getRequest().getIntValue("task_id");
    long focused_id = this.getRequest().getLongValue("futures_contract");
    long rec_id = this.getRequest().getLongValue("rec_id");
    switch (task_id)
	{
      case 1 : // Update
      	if(rec_id > 0)
      	{
      		focused_id = doUpdate(table_name,focused_id);
      	}
        break;
      case 2 : // Delete
        focused_id = doDelete(table_name,focused_id);
        focused_id = 1;
        break;
      default : // View only
        break;
      case 3 : // Close Out
      	focused_id = doCloseOut();
    }
    this.setFocusedID(focused_id);

    return focused_id;
  }  
  
  private long doCloseOut()
  {
  	String TranId = getRequest().getStringValue("sel_id");
  	java.sql.Date ClOutDt = Utils.strToDate(getRequest().getStringValue("close_out_date"));
  	float BotLot = getRequest().getFloatValue("tot_cl_bot");
  	float SoldLot = getRequest().getFloatValue("tot_cl_sold");
  	
  	
  	String StrSql="";
  	String CoRef = this.getNew_CoRef();
  	
  	int IdLen = TranId.length();
  	int loop=0;
  	long ret=0;
  
  	while ((loop+1) <= IdLen)
  	{
  		String OneVal = TranId.substring(loop,loop+1);
  		int id = 0;
  		float ton = 0;
  		float lot = 0;
  		String type = "";
  		if (OneVal.compareTo(",")!=0)
  		{
  			id = Utils.strToInt(OneVal);
  			ton = this.getValue("tons",id,"futures_contract");
  			lot = this.getValue("lots",id,"futures_contract");
  			type = this.getName("bot_sold",id,"futures_contract");
  			
  			StrSql = "insert futures_close_out " +
			"SET ref_number = ?, " +
			"close_out_date = ?, " +
			"transaction_id = ?, " +
			"type = ?, " +
  			"lot_closed = ?, " +
			"tons_closed = ?";

    		Vector vp = new Vector();
    		vp.addElement(new StringValue(CoRef));
    		vp.addElement(new DateValue(ClOutDt));
    		vp.addElement(new IntValue(id));
    		vp.addElement(new StringValue(type));
    		vp.addElement(new FloatValue(lot));
    		vp.addElement(new FloatValue(ton));
			ret = getSQLCommand().executeUpdate(StrSql,vp);
  		}
		loop=loop+1;
  	}
  	
  	return ret;
  }
  
  private String getNew_CoRef()
  {
    String sql =
        "SELECT MAX(ref_number) as ref_number FROM futures_close_out ";
    Record rc = new Record(this.getSQLCommand().executeQuery(sql));
    String ref = (!rc.isEmpty()) ? rc.getString("ref_number") : "CO 00-00000";
    rc.close();
    return "CO" + " " + this.getNextRefNumber(ref);
  }
  public ResultSet fetchFuturesList()
  {
  	//int Acc = getRequest().getIntValue("acc");
  	String BotSold = getRequest().getStringValue("bot_sold");
 	String TerMrk = getRequest().getStringValue("ter_mrk");
  	java.sql.Date TerMth = getRequest().getValueDate("ter_mth");//Utils.strToDate(getRequest().getStringValue("ter_mth"));
  	int BrokerId = getRequest().getIntValue("broker");
  	if (TerMrk.compareTo("")==0) TerMrk ="0";  	
  	if (BotSold.compareTo("")==0) BotSold ="0";
  	
//  	String Sql ="Select * from futures_contract where " +
//  				"(bot_sold = ? OR ?) " +
//				"and (terminal_market = ? OR ?) " +
//				"and (broker_id = ? OR ?) " +
//  				"and (terminal_month = ? OR ? OR (terminal_month IS NULL))";
  	
  	String Sql ="Select *,sum(futures_close_out.lot_closed) AS cllot from futures_contract " +
	    "LEFT OUTER JOIN futures_close_out ON (futures_contract.id=futures_close_out.transaction_id) " +
		"where (bot_sold = ? OR ?) " +
		"and (terminal_market = ? OR ?) " +
		"and (broker_id = ? OR ?) " +
		"and (terminal_month = ? OR ? OR (terminal_month IS NULL)) " +
		"GROUP BY futures_contract.id,futures_contract.ref_number, " +
		"futures_contract.transaction_date,futures_contract.bot_sold, " +
		"futures_contract.tons,futures_contract.lots,futures_contract.hedge_price, " +
		"futures_contract.differential,futures_contract.price,futures_contract.fixation_id, " +
		"futures_contract.terminal_month,futures_contract.terminal_market, " +
		"futures_contract.quality_id,futures_contract.broker_id,futures_contract.active order by futures_contract.id desc";
  	
    Vector vp = new Vector();

    vp.addElement(new StringValue(BotSold));
    vp.addElement(new BooleanValue(BotSold.compareTo("0") == 0));

    vp.addElement(new StringValue(TerMrk));
    vp.addElement(new BooleanValue(TerMrk.compareTo("0") == 0));

    vp.addElement(new IntValue(BrokerId));
    vp.addElement(new BooleanValue(BrokerId == 0));
    
    vp.addElement(new DateValue(TerMth));
    vp.addElement(new BooleanValue(TerMth.compareTo(new java.sql.Date(0)) == 0));
  	
  	return getSQLCommand().executeQuery(Sql,vp);
  }  
  
}
